package com.ruoyi.project.mall.api;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.mall.service.MallInvoiceApplyService;
import com.ruoyi.project.mall.service.MallInvoiceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/api/userInvoice")
public class ApiUserInvoiceController {

    @Autowired
    private MallInvoiceUserService mallInvoiceUserService;
    @Autowired
    private MallInvoiceApplyService mallInvoiceApplyService;

    /**
     * 获取默认发票抬头
     * @param paramMap
     * @return
     */
    @ResponseBody
    @RequestMapping("/getDefaultInvoice")
    public R getDefaultInvoice(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())


            ){
                return R.error("参数错误");
            }
            return mallInvoiceUserService.getDefaultInvoice(paramMap);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }

    /**
     * 申请开票
     * @param paramMap
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateInvoiceApply")
    public R updateInvoiceApply(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("orderId") || StringUtils.isBlank(paramMap.get("orderId").toString())
                    ||!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())
                    ||!paramMap.containsKey("invoiceId") || StringUtils.isBlank(paramMap.get("invoiceId").toString())

            ){
                return R.error("参数错误");
            }
            return mallInvoiceApplyService.updateInvoiceApply(paramMap);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }


    /***
     * 获取发票规格
     * @param paramMap
     * @return
     */
    @ResponseBody
    @RequestMapping("/getInvoiceSpec")
    public R getInvoiceSpec(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("orderId") || StringUtils.isBlank(paramMap.get("orderId").toString())
                    ||!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())

            ){
                return R.error("参数错误");
            }
            return mallInvoiceApplyService.getInvoiceSpec(paramMap);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }


    /***
     * 获取补票列表
     * @param paramMap
     * @return
     */
    @ResponseBody
    @RequestMapping("/getOrderInvoiceList")
    public R getOrderInvoiceList(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())

            ){
                return R.error("参数错误");
            }
            return mallInvoiceUserService.getOrderInvoiceList(paramMap);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }


    /***
     * 删除发票抬头
     * @param paramMap
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteUserInvoice")
    public R deleteUserInvoice(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())
                    ||!paramMap.containsKey("invoiceId") || StringUtils.isBlank(paramMap.get("invoiceId").toString())

            ){
                return R.error("参数错误");
            }
            return mallInvoiceUserService.deleteUserInvoice(paramMap);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }


    /***
     * 设置默认发票抬头
     * @param paramMap
     * @return
     */
    @ResponseBody
    @RequestMapping("/setUserInvoiceDefault")
    public R setUserInvoiceDefault(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())
                    ||!paramMap.containsKey("invoiceId") || StringUtils.isBlank(paramMap.get("invoiceId").toString())

            ){
                return R.error("参数错误");
            }
            return mallInvoiceUserService.setUserInvoiceDefault(paramMap);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }


    /**
     * 获取用户发票抬头列表
     * @param paramMap
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUserInvoiceList")
    public R getUserInvoiceList(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())

            ){
                return R.error("参数错误");
            }
            return mallInvoiceUserService.getUserInvoiceList(paramMap);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }

    /***
     *  用户添加发票抬头
     * @param paramMap
     * @return
     */
    @ResponseBody
    @RequestMapping("/saveUserInvoice")
    public R saveUserInvoice(@RequestBody Map<String,Object> paramMap) {
        try{
            if (!paramMap.containsKey("userId") || StringUtils.isBlank(paramMap.get("userId").toString())
                    ||!paramMap.containsKey("companyName")
                    ||!paramMap.containsKey("taxNum")
                    ||!paramMap.containsKey("address")
                    ||!paramMap.containsKey("bankName")
                    ||!paramMap.containsKey("bankCard")

            ){
                return R.error("参数错误");
            }
            return mallInvoiceUserService.saveUserInvoice(paramMap);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }
}
